import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/adapters.dart';

class HiveProperty<T> extends ValueNotifier<T> {
  final Box<dynamic> _box;
  final String key;
  final T defValue;

  HiveProperty({
    required Box<dynamic> box,
    required this.key,
    required this.defValue,
  })  : _box = box,
        super(box.get(key, defaultValue: defValue));

  @override
  set value(T newValue) {
    _box.put(key, newValue);
    super.value = newValue;
  }

  void reset() {
    _box.delete(key);
    super.value = defValue;
  }
}
